﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.MerchantsSettings
{
    public class MerReportController : Controller
    {
        //
        // GET: /MerReport/
        ServiceReference1.Service1Client DCXT = new ServiceReference1.Service1Client();

        public ActionResult Index()
        {

            return View();
        }
        int  operid;
        /// <summary>
        /// 获取当前操作员拥有商户的权限
        /// </summary>
        /// <returns></returns>
        public ActionResult getMer()
        {
            try
            {
                Page<Model.Order_Mer_T> page = new Page<Model.Order_Mer_T>();
                List<Model.Order_Mer_T> list = new List<Model.Order_Mer_T>();//定义商品分类设置
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }
                operid = user.OperID;
                string result = DCXT.GetMer(user.OperID, "", 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    list = ParseClass<Model.Order_Mer_T>.JsonList(jieguo);

                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = list//返回当前页的数据
                    };
                    return Json(list, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(searcho["Msg"].ToString());
                }
            }
            catch (Exception ex)
            {               
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }


        

        /// <summary>
        /// 营业报表
        /// </summary>
        /// <param name="MerID"></param>
        /// <param name="sTime"></param>
        /// <param name="eTime"></param>
        /// <returns></returns>
        public ActionResult SearchReportTJ(string MerID, string Customer, string sTime, string eTime)
        {
            List<Model.View_Order_DayReport> ls = new List<Model.View_Order_DayReport>();
            List<Model.View_Order_DayReport> List1 = new List<Model.View_Order_DayReport>();
            List<string> TypeArr = new List<string>();
            List<string> newTypeArr = new List<string>();
            List<TJB> tjbList = new List<TJB>();
            List<Model.Allreport_T> mo = new List<Model.Allreport_T>();
            string result = DCXT.GetDayReport_Dishes(MerID, Customer, sTime, eTime, GetYZM._YZM, 1);
            JObject job = JObject.Parse(result);
            List<Model.Order_Mer_T> list_Mer =new List<Model.Order_Mer_T> ();
            if (job["BS"].ToString() == "1")
            {
                IList<JToken> jt = job["rows"].Children().ToList();
                //ls = ParseClass<Model.View_Order_DayReport>.JsonList(jt);
                mo = ParseClass<List<Model.Allreport_T>>.JsonDeserialize(job["rows"].ToString()); 
                foreach (Model.Allreport_T item in mo)
                {
                    Model.Order_Mer_T m = new Model.Order_Mer_T();
                    m.MerID = item.Order_DayReport.MerID;
                    m.MerName = item.Order_DayReport.MerName;
                    if (m.MerName != null & m.MerName != "")//获取商户
                    { 
                    if (list_Mer.Count == 0)
                    {
                        list_Mer.Add(m);
                    }
                    else {
                        List<Model.Order_Mer_T> lis = list_Mer.FindAll(u=>u.MerID==m.MerID);
                        if (lis.Count==0)
                        {
                            list_Mer.Add(m);
                        }
                    }
                    }
                    TypeArr.Add(item.Order_DayReport.DishesTypeID);
                }
                if (list_Mer.Count>1)
                { 
                foreach(Model.Order_Mer_T j in list_Mer){
                    double bz = 0;
                    double s=0;
                    foreach (Model.Allreport_T g in mo)
                    {
                        if(g.Order_DayReport.MerID==j.MerID){
                             s+=(double) g.Order_DayReport.DishesPay * g.Order_DayReport.DishesCount;
                             bz += g.DelPackMon; 
                            j.HowCount = list_Mer.Count;
                        }

                    }
                    j.DelFormMon = s + bz * 100;
                }
                }

                foreach (var item in TypeArr)
                {
                    if (!newTypeArr.Contains(item))
                    {
                        newTypeArr.Add(item);
                    }
                }
                double total = 0;
                double totalMon = 0;
                foreach (var itype in newTypeArr)
                {
                    int fsje = 0, sjje = 0;
                    TJB tjb = new TJB();
                    List<Model.View_Order_DayReport> vod = new List<Model.View_Order_DayReport>();
                    List<DayR> ttr = new List<DayR>();
                    List<Model.Allreport_T> vod1 = new List<Model.Allreport_T>();
                    foreach (Model.Allreport_T ils in mo)//循环Allreport_T
                    {
                        
                        if (ils.Order_DayReport.DishesTypeID == itype)
                        {
                            //ils.DelPackMon = 1.0;
                            tjb.typeName = ils.Order_DayReport.DishesTypeName;
                            tjb.type = itype;
                            //fsje += ils.DishesMon * ils.DishesCount * 0.01;
                            //sjje += ils.DishesPay * ils.DishesCount * 0.01;
                            //tjb.fsje = fsje.ToString("0.00");
                            //tjb.sjje = sjje.ToString("0.00");
                            //tjb.zkje = (fsje - sjje).ToString("0.00");
                            fsje += ils.Order_DayReport.DishesMon * ils.Order_DayReport.DishesCount ;
                            sjje += ils.Order_DayReport.DishesPay * ils.Order_DayReport.DishesCount ;
                            tjb.fsje = fsje.ToString();
                            tjb.sjje = sjje.ToString();
                            tjb.zkje = (fsje - sjje).ToString();
                            tjb.count += ils.Order_DayReport.DishesCount;
                            tjb.bzf += ils.DelPackMon;
                            tjb.DelPackMon = ils.DelPackMon;
                            vod1.Add(ils);
                           
                        }
                    }
                    if (vod1.Count!=0)
                    {
                        for (int i = 0; i < vod1.Count;i++ )
                        {
                            DayR js = new DayR();
                            js.ST= vod1[i].DelPackMon.ToString();
                            js.DishesR = vod1[i].Order_DayReport;
                            ttr.Add(js);
                            
                            tjb.nej = ttr;
                            //vod.Add(vod1[i].Order_DayReport);
                            //tjb.DishesReoprt = vod;
                        }
                    }
                    
                    tjbList.Add(tjb);
                }
                TJB newtjb = new TJB();
                List<TJB> newtjbList = new List<TJB>();
                foreach (TJB item in tjbList)
                {
                    totalMon  += item.bzf*100;
                    total += double.Parse(item.sjje);
                    //newtjb.fsje = (int.Parse(item.fsje) * 0.01).ToString("0.01");
                    //newtjb.sjje = (int.Parse(item.sjje) * 0.01).ToString("0.01");
                    //newtjb.zkje = (int.Parse(item.zkje) * 0.01).ToString("0.01");
                }
                var data = new
                {
                    list_Mer=list_Mer,
                    totalMon=totalMon,
                    total = total,
                    //total = total.ToString("0.00"),
                    rows = tjbList//返回当前页的数据
                };
                return Json(data);
            }
            else
            {
                return Json(ls);
            }
        }

        /// <summary>
        /// 分类统计
        /// </summary>
        public class TJB : LSZ
        {
            /// <summary>
            /// 分类
            /// </summary>
            public string type { get; set; }
            /// <summary>
            /// 分类名称
            /// </summary>
            public string typeName { get; set; }
            /// <summary>
            /// 发生金额
            /// </summary>
            public string fsje { get; set; }
            /// <summary>
            /// 折扣金额
            /// </summary>
            public string zkje { get; set; }
            /// <summary>
            /// 实际金额
            /// </summary>
            public string sjje { get; set; }
            /// <summary>
            /// 菜品销售数量
            /// </summary>
            public int count { get; set; }
            /// <summary>
            /// 包装费
            /// </summary>
            public double bzf { get; set; }

        }
        public class LSZ
        {
            public double DelPackMon { get; set; }
            public List<DayR> nej { get; set; }
            //public List<Model.View_Order_DayReport> DishesReoprt { get; set; } 
        }

        public class DayR
        {
            public Model.View_Order_DayReport DishesR { get; set; }
            public string ST { get; set; }
         
        
        }
        //添加包装费
        //public class Allreport
        //{
        //    public double DelPackMon { get; set; }
        //    public Model.View_Order_DayReport Order_DayReport { get; set; }

        //}

    }
}
